package Queuedemo;

public class MyCircularQueue {
    public int[] elem;
    public int rear;
    public int front;
    public MyCircularQueue(int k) {
        elem = new int[k+1];
    }
//    入队
    public boolean enQueue(int val){
        if (isFull()){
            return false;
        }
        elem[rear] = val;
        rear = (rear + 1) % elem.length;
        return true;
    }
//    删除队头元素
    public boolean deQueue(){
        if (isEmpty()){
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }
    public int Front(){
        if (isEmpty()){
            return -1;
        }
        return elem[front];
    }

    public int Last(){
        if (isEmpty()){
            return -1;
        }
        int index = (rear == 0) ? elem.length-1 : rear-1;
        return elem[index];
    }
//    判断front和rear相遇
    public boolean isEmpty(){
        return front == rear;
    }
    public boolean isFull(){
        rear = (rear + 1) % elem.length;
        return (rear + 1) % elem.length == front;
    }
}
